﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace CrashSimul8or
{
    public class ParticleEngine
    {
        protected Random random;
        public Vector2 EmitterLocation { get; set; }
        protected List<Particle> particles;
        protected List<Texture2D> textures;
        protected int newParticlesPerUpdate;

        /// <summary>
        /// Einfaldur smiður fyrir particleEngine
        /// </summary>
        /// <param name="textures">Lista af textures sem á að nota sem particles</param>
        /// <param name="location">Hnitin á skjánum þar sem particleEngine á að sjást</param>
        /// <param name="newParticlesPerUpdate">Hversu þétt particlein eiga að vera</param>
        public ParticleEngine(List<Texture2D> textures, Vector2 location, int newParticlesPerUpdate)
        {
            EmitterLocation = location;
            this.textures = textures;
            this.particles = new List<Particle>();
            this.newParticlesPerUpdate = newParticlesPerUpdate;
            random = new Random();
        }

        /// <summary>
        /// Aðferð sem færir hvert particle og eyðir því ef það er búið með líftíma sinn.
        /// </summary>
        public virtual void Update()
        {
            for (int i = 0; i < newParticlesPerUpdate; i++)
            {
                particles.Add(GenerateNewParticle());
            }

            for (int particle = 0; particle < particles.Count; particle++)
            {
                particles[particle].Update();
                if (particles[particle].TTL <= 0)
                {
                    particles.RemoveAt(particle);
                    particle--;
                }
            }
        }

        /// <summary>
        /// Aðferð sem byr til particle með algjörlega random Velocity í allar
        /// áttir. Einnig er Random litur á þeim öllum.
        /// </summary>
        /// <returns>Tryllt particle</returns>
        protected virtual Particle GenerateNewParticle()
        {
            Texture2D texture = textures[random.Next(textures.Count)];
            Vector2 position = EmitterLocation;
            Vector2 velocity = new Vector2(
                                    1f * (float)(random.NextDouble() * 2 - 1),
                                    1f * (float)(random.NextDouble() * 2 - 1));
            float angle = 0;
            float angularVelocity = 0.1f * (float)(random.NextDouble() * 2 - 1);
            Color color = new Color(
                        (float)random.NextDouble(),
                        (float)random.NextDouble(),
                        (float)random.NextDouble());
            float size = (float)random.NextDouble();
            int ttl = 20 + random.Next(40);

            return new Particle(texture, position, velocity, angle, angularVelocity, color, size, ttl);
        }

        /// <summary>
        /// Aðferð sem teiknar upp allar particles í vélinni.
        /// </summary>
        /// <param name="spriteBatch">SpriteBatch til að teikna</param>
        /// <param name="cameraPos">Staðsetning myndavélar</param>
        public virtual void Draw(SpriteBatch spriteBatch, Vector2 cameraPos)
        {
            for (int index = 0; index < particles.Count; index++)
            {
                particles[index].Draw(spriteBatch);
            }
        }
    }
}
